from flask import Blueprint,jsonify, request
from main.BSL.userBSL import *
from main.utils import InterfaceHelper,RequestHelper
user = Blueprint('user',__name__)


@user.route('/user/', methods=["GET"])
def user_login():
    res = {}
    dic = RequestHelper.formToDict(request)
    is_exist = query_username_password(dic['username'], dic['password'])
    if is_exist:
        res = is_exist.to_json()
        res['status'] = 1
    else:
        res['status'] = 0
    return jsonify(res)


@user.route('/user/register', methods=["POST"])
def user_register():
    res = {}
    dic = RequestHelper.formToDict(request)
    user = JSONHelper.dictToObj(dic, User)
    is_success = insert(user)
    res['status'] = 1
    return jsonify(res)



